#include "mandate.h"

int pwd(int *id,char *buf_to_store_path,MYSQL* mysql){

    char sql[1024]={0};

    sprintf(sql,"select path from file_tbl where id = %d;",*id);

    int qret=mysql_query(mysql,sql);
    if(qret!=0){
        fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
    }

    MYSQL_RES* result = mysql_store_result(mysql);
    MYSQL_ROW row;
    row=mysql_fetch_row(result);

    strcpy(buf_to_store_path,row[0]);
    printf("current working directory is %s\n",buf_to_store_path);
    return 0;
}

int mkdir_server(char* dir_name,char* pwd,char* user_name,int* id,MYSQL* mysql){

    char sql[1024]={0};

    if(*id!=-1)
    {
        char *dir = strtok(dir_name,"/");
        strcat(pwd,dir);
        strcat(pwd,"/");
    }

    sprintf(sql,"insert into file_tbl (filename,user_name,pre_id,type,path,is_delete,md5)"
            "values('%s','%s',%d,'dir','%s',0,'0');",dir_name,user_name,*id,pwd);
    int qret=mysql_query(mysql,sql);
    if(qret!=0){
        fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
    }

    if(*id==-1)
    {
        bzero(sql,sizeof(sql));
        sprintf(sql,"select id from file_tbl where path = '%s';",pwd);

        qret=mysql_query(mysql,sql);
        if(qret!=0){
            fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
        }

        MYSQL_RES *res=mysql_store_result(mysql);
        MYSQL_ROW row;
        row=mysql_fetch_row(res);
        *id=atoi(row[0]);
    }

    return 0;
}
int remove_server(char* filename,char* pwd,char* user_name,MYSQL* mysql){

    char sql[1024]={0};
    sprintf(sql,"update file_tbl set is_delete = 1 where filename = '%s' and path= '%s' and user_name = '%s' and is_delete = 0;",filename,pwd,user_name);
    int qret=mysql_query(mysql,sql);
    if(qret!=0){
        fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
        return -1;
    }

    return 0;
}

int rmdir_server(char* filename,char* path,char* user_name,int* id,MYSQL* mysql){
    char sql[1024]={0};
    sprintf(sql,"select id from file_tbl where filename = '%s' and pre_id = %d and type = 'dir' and is_delete = 0;",filename,*id);
    int qret=mysql_query(mysql,sql);
    printf("81\n");
    if(qret!=0){
        fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
        return -1;
    }

    printf("82\n");
    MYSQL_RES* result = mysql_store_result(mysql);
    if(result == NULL)
    {
        return -1;
    }
    else
    {
        MYSQL_ROW delete_id = mysql_fetch_row(result);
        bzero(sql,sizeof(sql));
        sprintf(sql,"select id from file_tbl where pre_id = %d and is_delete = 0;",atoi(delete_id[0]));
        int qret=mysql_query(mysql,sql);
        if(qret!=0){
            fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
            return -1;
        }
        bzero(result,sizeof(MYSQL_RES));
        result = mysql_store_result(mysql);

        if(mysql_num_rows(result)==0)
        {
            bzero(sql,sizeof(sql));
            sprintf(sql,"update file_tbl set is_delete = 1 where id = %d;",atoi(delete_id[0]));
            qret=mysql_query(mysql,sql);
            if(qret!=0){
                fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
            }

            printf("dir with id = %id is deleted sucessfully!\n",atoi(delete_id[0]));
            return 0;
        }
        else
        {
            return -1;
        }
    }
}

int change_id(char* user_name,int *id,int pre_id,MYSQL* mysql){

    char sql[1024]={0};
    sprintf(sql,"select id from file_tbl where user_name = '%s' and pre_id = %d;",user_name,pre_id);

    int qret=mysql_query(mysql,sql);
    if(qret!=0){
        fprintf(stderr,"SQL statement error : %s\n",mysql_error(mysql));
    }

    MYSQL_RES *res=mysql_store_result(mysql);
    MYSQL_ROW row;
    row=mysql_fetch_row(res);
    *id=atoi(row[0]);

    return 0;
}

#ifdef DEBUG
int main(int argc,char* argv[])
{

    MYSQL *mysql=mysql_init(NULL);

    MYSQL* ret=mysql_real_connect(mysql,"localhost","root","1","server_db",0,NULL,0);
    if(ret==NULL){
        fprintf(stderr,"mysql connect error: %s\n",mysql_error(mysql));
    }
    printf("mysql connect sucessfully!\n");

   // int qret= remove_server("giao","sdf/fdsf/","rap",mysql);
   // ERROR_CHECK(qret,-1,"remov_server");
    int id=3;
    char buf[1024]={0};
   int qret=pwd(&id,buf,mysql);
     id=112;
   ERROR_CHECK(qret,-1,"pwd");
    qret=rmdir_server("giao","sdf/fdsf/","rap",&id,mysql);
    ERROR_CHECK(qret,-1,"rmdir_server");
    //char sql[256]={0};
    //sprintf(sql,"select * from pet;");
    //int qret=mysql_query(mysql,sql);
    //if(qret!=0){
    //fprintf(stderr,"SQL statement error: %s\n",mysql_error(mysql));
    //}
    //
    //MYSQL_RES *res=mysql_store_result(mysql);
    //printf("rows= %ld, columns= %d\n",mysql_num_rows(res),mysql_num_fields(res));
    //MYSQL_ROW row;
    //while((row=mysql_fetch_row(res))!=NULL){
    //    for(unsigned int i=0;i<mysql_num_fields(res);++i){
    //        printf("%s\t",row[i]);
    //    }
    //    printf("\n");
    //}

    while(1);

    return 0;
}
#endif


